package net.cz.wc.command;

import java.sql.Connection;
import java.util.List;

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

import net.cz.wc.dao.BasicStatsDTO;
import net.cz.wc.dao.SQLDao;
import net.cz.wc.data.ConnectionManager;
import net.cz.wc.main.Context;
import net.cz.wc.output.ConsoleOutput;

public class StatsCommand implements Command {
	
	public void execute(String[] args) {
		System.out.println("StatsCommand.execute()");
		
		this.generateBasicStats();
	}
	
	private void generateBasicStats() {
		Connection conn = null;
		
		try {
			conn = ConnectionManager.getConnection();
			SQLDao dao = new SQLDao(conn);
			List<Long> valueList = dao.getValues(null);
			
			DescriptiveStatistics descStats = new DescriptiveStatistics();
			for (Long value : valueList) {
				descStats.addValue(value);
			}
			
			BasicStatsDTO statsDTO = new BasicStatsDTO();
			statsDTO.setMean(descStats.getMean());
			statsDTO.setMax(descStats.getMax());
			statsDTO.setMin(descStats.getMin());
			statsDTO.setStandardDeviation(descStats.getStandardDeviation());
			statsDTO.setCount(descStats.getN());
			
			ConsoleOutput.print(statsDTO);
		} catch (Exception ee) {
			Context.getContext().printException(ee);
		} finally {
			ConnectionManager.closeConnection(conn);
		}
	}
}
